package com.lcx.plugin.neteaseCloud.music;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lcx.plugin.neteaseCloud.component.toolwindow.MainPanel;
import com.lcx.plugin.neteaseCloud.enums.LoginTypeEnum;
import com.lcx.plugin.neteaseCloud.music.entity.LyricEntity;
import com.lcx.plugin.neteaseCloud.music.entity.MusicListEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据中心，存储数据
 *
 * @author daxiong
 * @date 2021/9/10 上午9:38
 */
public class DataCenter {
    /**
     * 用户头像
     */
    public static String userImageUrl;

    /**
     * 登录方式。1：邮箱；2：手机；3：验证码；4：二维码
     *
     * @see LoginTypeEnum
     */
    public static Integer loginType;

    /**
     * 账号
     */
    public static String accountNumber;

    /**
     * 密码
     */
    public static String accountPassword;
    /**
     * 国家码
     */
    public static String countryCode;

    /**
     * 手机验证码
     */
    public static String phoneCode;

    /**
     * 个人信息
     */
    public static JSONObject userProfile;

    /**
     * 用户信息
     */
    public static JSONObject userInfo = new JSONObject();

    /**
     * 个人id
     */
    public static String uid;

    /**
     * cookie
     */
    public static String cookie;

    /**
     * 歌单列表
     */
    public static JSONArray musicList;

    /**
     * 正在播放的歌曲实体
     */
    public static MusicListEntity playingSongEntity;
    /**
     * 播放队列
     */
    public static List<MusicListEntity> queueList = new LinkedList<>();
    /**
     * 播放模式。0：顺序播放；1：单曲循环
     */
    public static int playMode = 0;
    /**
     * 歌词列表
     */
    public static List<LyricEntity> lyricList = new ArrayList<>();
    /**
     * 要显示的歌词列表
     */
    public static List<LyricEntity> showLyricList = new LinkedList<>();
    /**
     * 歌词计数器
     */
    public static int lyricCounter = 0;

    /**
     * 主面板，要放在最后
     */
    public static MainPanel mainPanel = new MainPanel();

    public static Map<String, Object> transfer2Map() {
        Map<String, Object> result = new HashMap<>(1 << 4);
        result.put("userImageUrl", userImageUrl);
        result.put("loginType", loginType);
        result.put("accountNumber", accountNumber);
        result.put("accountPassword", accountPassword);
        result.put("countryCode", countryCode);
        result.put("phoneCode", phoneCode);
        result.put("userInfo", userInfo);
        result.put("userProfile", userProfile);
        result.put("uid", uid);
        result.put("cookie", cookie);
        result.put("musicList", musicList);
        result.put("playingSongEntity", playingSongEntity);
        result.put("queueList", queueList);
        return result;
    }

    public static void map2Attr(Map<String, Object> map) {
        userImageUrl = (String) map.get("userImageUrl");
        loginType = (Integer) map.get("loginType");
        accountNumber = (String) map.get("accountNumber");
        accountPassword = (String) map.get("accountPassword");
        countryCode = (String) map.get("countryCode");
        phoneCode = (String) map.get("phoneCode");
        userInfo = (JSONObject) map.get("userInfo");
        userProfile = (JSONObject) map.get("userProfile");
        uid = (String) map.get("uid");
        cookie = (String) map.get("cookie");
        musicList = (JSONArray) map.get("musicList");
        Map entity = (Map) map.get("playingSongEntity");
        playingSongEntity = BeanUtil.copyProperties(entity, MusicListEntity.class);
        List<Map> list = (List<Map>) map.get("queueList");
        queueList = list.stream()
                .map(q -> BeanUtil.copyProperties(q, MusicListEntity.class))
                .collect(Collectors.toList());
        playMode = 0;
    }
}
